package com.project.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.project.bean.MainPostBean;
import com.project.bean.UserBean;
import com.project.service.IMainPostService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.time.LocalDate;
import java.util.List;

@RestController
@RequestMapping("mainPost")
public class MainPostController {

    @Autowired
    private IMainPostService service;

    @RequestMapping("cutByItem")
    public IPage<MainPostBean> cutByItem(Integer id, Integer pageNo, String title, String author, LocalDate startDate,
                                         LocalDate endDate) {
        return service.cutByItem(id, pageNo, title, author, startDate, endDate);
    }

    /**
     * 为当前频道添加主贴
     *
     * @param postBean 主贴对象
     */
    @RequestMapping("add")
    public String add(MainPostBean postBean, HttpServletRequest req) {
        UserBean loginUser = (UserBean) req.getSession().getAttribute("loginUser");
        postBean.setAuthor(loginUser.getName());
        postBean.setUserId(loginUser.getId());
        service.add(postBean);
        return "ok";
    }

    @RequestMapping("findById")
    public MainPostBean findById(Integer postId) {
        return service.findById(postId);
    }
}
